package org.tostring.pork;

import javax.inject.Inject;
import javax.validation.Valid;

import org.pork.domain.EmployeesVO;
import org.pork.service.EmployeesService;
import org.pork.util.AjaxUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/regist")
@SessionAttributes("EmployeesVO")
public class RegistMemberControl {
	
	
	@ModelAttribute
	public void ajaxAttribute(WebRequest request, Model model) {
		model.addAttribute("ajaxRequest", AjaxUtil.isAjaxRequest(request));
	}

	@ModelAttribute("EmployeesVO")
	public EmployeesVO createMemberVO() {
		return new EmployeesVO();
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public void regist() {
	}

	@RequestMapping(method=RequestMethod.POST)
	public String processSubmit(@Valid EmployeesVO vo, BindingResult result, 
								@ModelAttribute("ajaxRequest") boolean ajaxRequest, 
								Model model, RedirectAttributes redirectAttrs) {
		
		
		
/*		if (result.hasErrors()) {
			
			String message = "success" + vo;
			System.out.println("has errors....");
			
			return null;
				
		
		try {
			employees.regist(vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		}
		if (ajaxRequest) {
			model.addAttribute("message", message);
			return null;
		} else {			
			redirectAttrs.addFlashAttribute("message", message);
			return "redirect:/regist";			
		}*/
		
		if (result.hasErrors()) {
			
			System.out.println("has errors....");
			
			return null;
		}
		
		String message = "success" + vo;
		
		if (ajaxRequest) {
			model.addAttribute("message", message);
			return null;
			
		} else {
			
			redirectAttrs.addFlashAttribute("message", message);
			return "redirect:/regist";	
		}
	}
	
}
